use crate::{Info, SqlConfig};
use sqlx::mysql::MySqlPoolOptions;

/// 获取所有表的相关数据
pub(crate) async fn fetch_data(config: &SqlConfig) -> Result<Vec<Info>, sqlx::Error> {
    let url = config.to_mysql_option();
    let pool = MySqlPoolOptions::new().connect(&url).await?;
    sqlx::query_as::<_, Info>(
        r#"
            select ORDINAL_POSITION, TABLE_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_NAME, COLUMN_COMMENT
            from information_schema.COLUMNS
            where TABLE_SCHEMA = ?
            order by TABLE_NAME;
        "#,
    )
    .bind(&config.database)
    .fetch_all(&pool)
    .await
}
